home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / X.C < prev    next >
C/C++ Source or Header  |  1997-08-18  |  7KB  |  300 lines

  1. #include "global.h"
  2. #include "commands.h"
  3. #include "mbuf.h"
  4. #include "netuser.h"
  5. #include "tcp.h"
  6. #include "domain.h"
  7. #include "x.h"
  8. #include "stats.h"
  9.  
  10. #if !defined(_lint)
  11. static char rcsid[] OPTIONAL = "$Id: x.c,v 1.17 1997/08/19 01:19:22 root Exp root $";
  12. #endif
  13.  
  14. #ifdef XSERVER
  15.  
  16. #ifdef HOLDMONITOR
  17. extern int HOLDindicator;
  18. #endif
  19. #ifdef ALERTMONITOR
  20. extern int ALERTindicator;
  21. #endif
  22.  
  23. static struct xcontrol *Xclients = NULLX;
  24. static struct xhost *Xhosts = NULLXHOST;
  25.  
  26. #ifdef ALLSERV
  27. extern int ScreenedCall;
  28. extern char *ScreenedCaller;
  29. #endif
  30. static int Sx = -1;
  31. static const char xlistener[] = "Xwindows listener";
  32. static const char xserver[] = "Xwindows server";
  33. void variable_expansion (struct mbuf **bp);
  34.  
  35. #ifdef CONVERS
  36. int CountConfUsers (void);
  37. #endif
  38. extern struct cmds Cmds[];
  39. extern char *NextCmdMsg;
  40. extern short NextCmd;
  41.  
  42.  
  43. static int xhostlookup (char *name);
  44. static void xserv (int s, void *ds, void *p);
  45. static char *xhostchk (int s);
  46. static int doxhosts (int argc,char *argv[],void *p);
  47. static int doxconnected (int argc,char *argv[],void *p);
  48.  
  49.  
  50. /* xwindows subcommand table */
  51. static struct cmds Xtab[] = {
  52.     { "connected",        doxconnected,        0, 0, NULLCHAR },
  53.     { "hosts",        doxhosts,        0, 0, NULLCHAR },
  54.     { NULLCHAR,        NULL,            0, 0, NULLCHAR }
  55. };
  56.  
  57.  
  58.  
  59. int
  60. doxwindows (int argc, char *argv[], void *p)
  61. {
  62.     return subcmd(Xtab,argc,argv,p);
  63. }
  64.  
  65.  
  66.  
  67. static int
  68. doxhosts (int argc, char *argv[], void *p OPTIONAL)
  69. {
  70. struct xhost *x;
  71. int i;
  72.  
  73.     if(argc == 1)        {     /*just list them*/
  74.         for (x = Xhosts; x != NULLXHOST; x = x->next)
  75.             tprintf("%s ", x->host);
  76.         tputc('\n');
  77.     } else {     /* add some xhost(s) */
  78.         for(i = 1; i < argc; i++) {
  79.             x = callocw(1,sizeof(struct xhost));
  80.                     x->host = strdup(argv[i]);
  81.                        /* add to list */
  82.                     x->next = Xhosts;
  83.                        Xhosts = x;
  84.             }
  85.            }
  86.            return 0;
  87. }
  88.  
  89.  
  90.  
  91. static int
  92. doxconnected (int argc OPTIONAL, char *argv[] OPTIONAL, void *p OPTIONAL)
  93. {
  94. struct xcontrol *x;
  95.  
  96.     for (x = Xclients; x != NULLX; x = x->next)
  97.         tprintf("%s ", x->host);
  98.     tputc('\n');
  99.            return 0;
  100. }
  101.  
  102.  
  103.  
  104. int
  105. xserver0 (int argc OPTIONAL, char *argv[] OPTIONAL, void *p OPTIONAL)
  106. {
  107.     return (deleteserver (&Sx));
  108. }
  109.  
  110.  
  111.  
  112. int
  113. xserver1 (int argc, char *argv[], void *p OPTIONAL)
  114. {
  115. int port = IPPORT_X;
  116.  
  117.     if (argc > 1)    {
  118.         port = atoi (argv[1]);
  119.         tprintf ("Starting Xserver on port %d\n", port);
  120.     }
  121.     return (installserver (argc, argv, &Sx, xlistener, port,
  122.         INADDR_ANY, xserver, xserv, 512, NULL));
  123.  
  124. }
  125.  
  126.  
  127.  
  128. static void
  129. xserv (int s, void *ds OPTIONAL, void *p OPTIONAL)
  130. {
  131. struct mbuf *bp;
  132. int datalen;
  133. struct xcontrol *x;
  134.  
  135.     x = (struct xcontrol *) mallocw (sizeof (struct xcontrol));
  136.     if (x == NULLX)
  137.         return;
  138.  
  139.     (void) sockowner(s,Curproc);    /* We own it now */
  140.     /* Secede from the parent's sockets, and use the network socket that
  141.      * was passed to us for both input and output. The reference
  142.      * count on this socket will still be 1; this allows the domboxbye()
  143.      * command to work by closing that socket with a single call.
  144.      * If we return, the socket will be closed automatically.
  145.      */
  146.     close_s(Curproc->output);
  147.     close_s(Curproc->input);
  148.     Curproc->output = Curproc->input = x->s = s;
  149.  
  150.     if ((x->host = xhostchk (s)) == NULLCHAR)    {
  151.         usprintf (s,"TNOS XSession denied! No permission........\n");
  152.         log (s, "XServer access denied!");
  153.     } else    {
  154.     
  155.         log(s, "open Xserver");
  156. #ifdef STATS_USE
  157.         STATS_adduse (1);
  158.         MiscUsers++;
  159. #endif
  160.         x->txstatusinfo = 0;
  161.         x->next = Xclients;
  162.         Xclients = x;
  163.         netPrompt();
  164.     
  165.         /* Process input on the connection */
  166.         for ( ; ; )    {
  167.             if((datalen = socklen(s,0)) > 0)    {
  168.                 if (recv_mbuf(s,&bp,0,NULLCHAR,0) == -1)
  169.                     break;
  170.                 variable_expansion (&bp);
  171.                 if (!strnicmp ((char *)bp->data, "ex", 2))
  172.                     break;
  173.                 if (!strnicmp ((char *)bp->data, "bbs", 2))    {
  174.                     tprintf (SCMD"start_bbs_session"ECMD);
  175.                     continue;
  176.                 }
  177.                 if (!strnicmp ((char *)bp->data, "txstat", 6))    {
  178.                     char *cp;
  179.                     cp = skipnonwhite ((char *)bp->data);
  180.                     cp = skipwhite (cp);
  181.                     x->txstatusinfo = atoi(cp);
  182.                     xnotify (X_ALL);
  183.                 } else
  184.                     (void)cmdparse(Cmds, (char *)bp->data, NULL);
  185.                 free_p(bp);
  186.                 netPrompt();
  187.             } else if(datalen < 0)
  188.                 break;
  189.             if (!datalen)
  190.                 (void) kpause (500);
  191.         }
  192. #ifdef STATS_USE
  193.         MiscUsers--;
  194. #endif
  195.         free (x->host);
  196.  
  197.         if (Xclients == x)
  198.             Xclients = x->next;
  199.         else    {
  200.             struct xcontrol *xp;
  201.  
  202.             for (xp = Xclients; xp != NULLX; xp = xp->next)    {
  203.                 if (xp->next == x)    {
  204.                     xp->next = x->next;
  205.                     break;
  206.                 }
  207.             }
  208.         }
  209.     }
  210.     close_s(s);
  211.     s = -1;
  212. }
  213.  
  214.  
  215.  
  216. void
  217. xnotify (int which)
  218. {
  219. struct xcontrol *x;
  220.  
  221.     for (x = Xclients; x != NULLX; x = x->next)    {
  222.         if (!x->txstatusinfo)
  223.             continue;
  224.         if (which & X_BBS)
  225.             usprintf (x->s, SCMD"set STATbbs %d"ECMD, BbsUsers);
  226. #ifdef CONVERS
  227.         if (which & X_CONF)
  228.             usprintf (x->s, SCMD"set STATconf %d"ECMD, CountConfUsers());
  229. #endif
  230.         if (which & X_FTP)
  231.             usprintf (x->s, SCMD"set STATftp %d"ECMD, FtpUsers);
  232.         if (which & X_TUT)
  233.             usprintf (x->s, SCMD"set STATtut %d"ECMD, Tutored);
  234. #ifdef ALLSERV        
  235.         if (which & X_SCREEN)  {
  236.             usprintf (x->s, SCMD"set STATscreencaller %s"ECMD,
  237.                 (ScreenedCaller) ? ScreenedCaller : "(none)");
  238.             usprintf (x->s, SCMD"set STATscreen %d"ECMD, ScreenedCall);
  239.         }
  240. #endif    
  241. #ifdef HOLDMONITOR
  242.         if (which & X_HOLD)
  243.             usprintf (x->s, SCMD"set STAThold %d"ECMD, HOLDindicator);
  244. #endif
  245. #ifdef ALERTMONITOR
  246.         if (which & X_ALERT)
  247.             usprintf (x->s, SCMD"set STATalert %d"ECMD, ALERTindicator);
  248. #endif
  249.         if (which & X_MSG)    {
  250.             char *cp, *cp2;
  251.             cp = strdup ((NextCmd) ? NextCmdMsg : "");
  252.             while ((cp2 = strpbrk (cp, "[]{}")) != NULLCHAR)
  253.                 *cp2 = ' ';
  254.             usprintf (x->s, SCMD"set STATmsg { %s}"ECMD, cp);
  255.             free (cp);
  256.         }
  257.     }
  258. }
  259.  
  260.  
  261.  
  262. static char *
  263. xhostchk (int s)
  264. {
  265. struct sockaddr fsocket;
  266. int i, trans;
  267. char *retval = NULLCHAR;
  268. char *cptr, *cptr2;
  269.  
  270.     i = SOCKSIZE;
  271.     trans = DTranslate; /* Save IP address translation state */
  272.     DTranslate = 0;     /* Force output to be numeric IP addr*/
  273.     if (getpeername(s,(char *)&fsocket,&i) != -1)    {
  274.         cptr = psocket(&fsocket);
  275.         if ((cptr2 = strchr(cptr, ':')) != NULLCHAR)
  276.             *cptr2 = 0;
  277.         if (xhostlookup (cptr))
  278.             retval = strdup (cptr);
  279.     }
  280.     DTranslate = trans;             /* Restore original state */
  281.     return retval;
  282. }
  283.  
  284.  
  285.  
  286. static int
  287. xhostlookup (char *name)
  288. {
  289. struct xhost *x;
  290.  
  291.     for (x = Xhosts; x != NULLXHOST; x = x->next) {
  292.         if (!stricmp (x->host, name))
  293.             return 1;
  294.     }
  295.     return 0;
  296. }
  297.  
  298.  
  299. #endif /* XSERVER */
  300.